package com.hqyj.product.controller;

import com.hqyj.product.model.ProductInfo;
import com.hqyj.sale.model.SaleInfo;
import frame.product.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/productController")
public class productController extends BaseController {
    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, ProductInfo productInfo) {
        //1.接收数据;
        System.out.println("已经进入controller");
//        System.out.println("-----------list-------");
        //3.调用业务;
        List<ProductInfo> productInfo_dbList = productInfoService.queryAllProductInfo();
        System.out.println(productInfo_dbList);
        for (ProductInfo info : productInfo_dbList) {
            System.out.println("数据库对象"+info);
        }
        model.addAttribute("productInfo_dbList", productInfo_dbList);
        //2.跳转页面;
        return "view/product/productInfo/productInfoList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, ProductInfo productInfo) {
        //1.接收数据;
//        System.out.println("-----------add------" + productInfo);
        //3.调用业务;
        int n = productInfoService.addProductInfoByProductInfo(productInfo);

        //2.跳转页面;
        List<ProductInfo> productInfo_dbList = productInfoService.queryAllProductInfo();
        model.addAttribute("productInfo_dbList", productInfo_dbList);
        return "/view/product/productInfo/productInfoList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, ProductInfo productInfo, String idStr) {
        //1.接收数据;
        System.out.println("-----------delete------" + idStr);
        //3.调用业务;
        //字符类型转数字类型
        int id = Integer.parseInt(idStr);
        //业务
        int n = productInfoService.deleteProductInfoById(id);

        //2.跳转页面;
        List<ProductInfo> productInfo_dbList = productInfoService.queryAllProductInfo();
        model.addAttribute("productInfo_dbList", productInfo_dbList);
        return "view/product/productInfo/productInfoList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, ProductInfo productInfo, String idStr) {
        //字符类型转数字类型
        int productInfoId = Integer.parseInt(idStr);
        //业务
        ProductInfo productInfo_db = productInfoService.queryProductInfoById(productInfoId);
        System.out.println("-----------updateUI------" + productInfo_db);
        model.addAttribute("productInfo", productInfo_db);
        //2.跳转页面;
        return "view/product/productInfo/updateProductInfo";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, ProductInfo productInfo, String id) {
        //1.接收数据;
        //字符类型转数字类型
        int productInfoId = Integer.parseInt(id);

        //3.调用业务;
        int n = productInfoService.updateProductInfoByProductInfo(productInfo);


        //2.跳转页面;
        List<ProductInfo> productInfo_dbList = productInfoService.queryAllProductInfo();
        model.addAttribute("productInfo_dbList", productInfo_dbList);
        return "view/product/productInfo/productInfoList";
    }

    @RequestMapping("/send.do")
    public String send(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, ProductInfo productInfo, String idStr) {

        ProductInfo productInfo_db = productInfoService.queryProductInfoById(Integer.parseInt(idStr));
        String type = "产品";
        //调用业务;
        int m = productInfoService.updateRepertoryByProductInfo(productInfo_db.getProductName(),productInfo_db.getProductQuantity());
        int n = productInfoService.insertInRepertoryByProductInfo(productInfo_db.getProductId(),productInfo_db.getProductName(),productInfo_db.getProductQuantity(),type);

        //修改状态
        productInfoService.updateStateByProductId(Integer.parseInt(idStr));

        //2.跳转页面;
        List<ProductInfo> productInfo_dbList = productInfoService.queryAllProductInfo();
        model.addAttribute("productInfo_dbList", productInfo_dbList);
        return "view/product/productInfo/productInfoList";
    }
}
